<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>bucket_script 聚合 | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'search-aggregations-pipeline-bucket-script-aggregation.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-pipeline-bucket-script-aggregation.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-pipeline-bucket-script-aggregation.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/search-aggregations-pipeline-bucket-script-aggregation.html" rel="nofollow" target="_blank">../en/search-aggregations-pipeline-bucket-script-aggregation.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch 权威指南 [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations.html">聚合</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations-pipeline.html">管道聚合</span>
»
<span class="breadcrumb-node">bucket_script 聚合</span>
</div>
<div class="navheader">
<span class="prev">
<a href="search-aggregations-pipeline.html">« 管道聚合</a>
</span>
<span class="next">
<a href="search-aggregations-pipeline-bucket-selector-aggregation.html">bucket_selector 聚合 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="search-aggregations-pipeline-bucket-script-aggregation"></a>桶脚本(bucket_script)聚合
</h2>
</div></div></div>
<p>
执行脚本的父管道聚合，该脚本可以对父级多桶聚合中的特定的度量按桶进行计算。

指定的度量必须是数字的，脚本必须返回一个数值。 
</p>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="bucket-script-agg-syntax"></a>语法
</h3>
</div></div></div>
<p>一个单独的<code class="literal">bucket_script</code>看起来像这样：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
    "bucket_script": {
        "buckets_path": {
            "my_var1": "the_sum", <a id="CO252-1"></a><i class="conum" data-value="1"></i>
            "my_var2": "the_value_count"
        },
        "script": "params.my_var1 / params.my_var2"
    }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO252-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>这里，<code class="literal">my_var1</code>是脚本中使用的 buckets_path 的变量名，<code class="literal">the_sum</code>是该变量使用的度量的路径。</p>
</td>
</tr>
</table>
</div>
<div class="table">
<a id="bucket-script-params"></a>
<p class="title"><strong>表 7. <code class="literal">bucket_script</code> 参数</strong></p>
<div class="table-contents">
<table border="1" cellpadding="4px" summary="bucket_script Parameters">
<colgroup>
<col class="col_1">
<col class="col_2">
<col class="col_3">
<col class="col_4">
</colgroup>
<thead>
<tr>
<th align="left" valign="top">参数名称</th>
<th align="left" valign="top">描述</th>
<th align="left" valign="top">是否必需</th>
<th align="left" valign="top">默认值</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left" valign="top"><p><code class="literal">script</code></p></td>
<td align="left" valign="top"><p>
为此聚合运行的脚本。脚本可以是内联、文件或索引化的。(更多详情请参考 <a class="xref" href="modules-scripting.html" title="脚本" rel="nofollow">脚本</a>)
</p></td>
<td align="left" valign="top"><p>必需</p></td>
<td align="left" valign="top"><p></p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">buckets_path</code></p></td>
<td align="left" valign="top"><p>
脚本变量及其关联路径到我们希望用于变量的桶的映射(更多详情请参考 <a class="xref" href="search-aggregations-pipeline.html#buckets-path-syntax" title="buckets_path语法" rel="nofollow"><code class="literal">buckets_path</code>语法</a>)
</p></td>
<td align="left" valign="top"><p>必需</p></td>
<td align="left" valign="top"><p></p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">gap_policy</code></p></td>
<td align="left" valign="top"><p>
在数据中发现间隙时应用的策略(更多详情请参考<a class="xref" href="search-aggregations-pipeline.html#gap-policy" title="处理数据中的间隙">处理数据中的间隙</a>)
 </p></td>
<td align="left" valign="top"><p>可选</p></td>
<td align="left" valign="top"><p><code class="literal">skip</code></p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">format</code></p></td>
<td align="left" valign="top"><p>应用于此聚合的输出值的格式</p></td>
<td align="left" valign="top"><p>可选</p></td>
<td align="left" valign="top"><p><code class="literal">null</code></p></td>
</tr>
</tbody>
</table>
</div>
</div>
<p>下面这个代码片段计算了每个月的T恤衫的销售额占总销售额的百分比：</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /sales/_search
{
    "size": 0,
    "aggs" : {
        "sales_per_month" : {
            "date_histogram" : {
                "field" : "date",
                "calendar_interval" : "month"
            },
            "aggs": {
                "total_sales": {
                    "sum": {
                        "field": "price"
                    }
                },
                "t-shirts": {
                  "filter": {
                    "term": {
                      "type": "t-shirt"
                    }
                  },
                  "aggs": {
                    "sales": {
                      "sum": {
                        "field": "price"
                      }
                    }
                  }
                },
                "t-shirt-percentage": {
                    "bucket_script": {
                        "buckets_path": {
                          "tShirtSales": "t-shirts&gt;sales",
                          "totalSales": "total_sales"
                        },
                        "script": "params.tShirtSales / params.totalSales * 100"
                    }
                }
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/549.console"></div>
<p>响应可能是这样的：</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
   "took": 11,
   "timed_out": false,
   "_shards": ...,
   "hits": ...,
   "aggregations": {
      "sales_per_month": {
         "buckets": [
            {
               "key_as_string": "2015/01/01 00:00:00",
               "key": 1420070400000,
               "doc_count": 3,
               "total_sales": {
                   "value": 550.0
               },
               "t-shirts": {
                   "doc_count": 1,
                   "sales": {
                       "value": 200.0
                   }
               },
               "t-shirt-percentage": {
                   "value": 36.36363636363637
               }
            },
            {
               "key_as_string": "2015/02/01 00:00:00",
               "key": 1422748800000,
               "doc_count": 2,
               "total_sales": {
                   "value": 60.0
               },
               "t-shirts": {
                   "doc_count": 1,
                   "sales": {
                       "value": 10.0
                   }
               },
               "t-shirt-percentage": {
                   "value": 16.666666666666664
               }
            },
            {
               "key_as_string": "2015/03/01 00:00:00",
               "key": 1425168000000,
               "doc_count": 2,
               "total_sales": {
                   "value": 375.0
               },
               "t-shirts": {
                   "doc_count": 1,
                   "sales": {
                       "value": 175.0
                   }
               },
               "t-shirt-percentage": {
                   "value": 46.666666666666664
               }
            }
         ]
      }
   }
}</pre>
</div>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="search-aggregations-pipeline.html">« 管道聚合</a>
</span>
<span class="next">
<a href="search-aggregations-pipeline-bucket-selector-aggregation.html">bucket_selector 聚合 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>